MyDesktop.ExpenseWindow = Ext.extend(Ext.app.Module, {
    id:'expense-win',
    init : function(){
        this.launcher = {
            text: '费用项目',
            iconCls:'icon-grid',
            handler : this.createWindow,
            scope: this
        }
    },
    partnerRecord : Ext.data.Record.create([
	    {name : 'id',mapping : 'id',type : 'int'}, 	    
	    {name : 'name',mapping : 'name',type : 'string'}, 
	    {name : 'pinyin',mapping : 'pinyin',type : 'string'},
	    {name : 'expense',mapping : 'expense',type : 'double'}   
	   ]),
	cm_partner : new Ext.grid.ColumnModel([
	       new Ext.grid.RowNumberer(),
		   {header : '名称', width : 120,dataIndex : 'name'},
		   {header : '简拼',width : 120,dataIndex : 'pinyin'},
		   {header : '金额',width : 40,dataIndex : 'expense'}
	]),

	getPanel:function(){
		 	 var store=new Ext.data.Store({
				 url : app.url+'/admin/com/expenseItems.do',
				 baseParams:{start:0,limit:20},
				 autoLoad : true,
                 reader: new Ext.data.JsonReader({totalProperty : 'totalProperty',root : 'root'}, this.partnerRecord)
             });	
       		 
       		 var partner_detailForm = new Ext.FormPanel({
       		 	 labelSeparator : "：",
				 frame:true,
				 border:false,
				 defaultType : 'textfield',
				 defaults:{width:160},
				 items:[{
			              xtype:'hidden',
			              name:'id'			              			
			          },{				     	
				      	fieldLabel:'项目名称',
				      	name:"name",
						allowBlank : false,
						minLength : 2,
			            maxLength : 15
						
				     },{				     	
				     	fieldLabel:'项目费用',
				      	name:"expense",
						xtype : "numberfield",
						allowBlank : false,
						minLength : 1,
			            maxLength : 8
				     }], 
				 buttons:[
			       {
			     	id:'commitButton',
			     	iconCls : 'save',
			        handler:submitForm
			       },{
			     	 id:'closeButton',
					 text : '关闭',
					 iconCls : 'close',
			     	 handler:function(){				     		
			     	    addOrEditPartner_win.hide();
			     	}
			      }
			     ]
       		 });
       		 
       		 var addOrEditPartner_win = new Ext.Window({
       		 	layout:'fit',
		    	width:330,
		    	closeAction:'hide',
		   		height:150,
				resizable :false,
				shadow : true,
				modal :true,
		   	    closable:true,
		    	bodyStyle:'padding:5 5 5 5',		    	
		    	animCollapse:true,
				items:[partner_detailForm]
       		 });
       		 
       		 var partner_gridPanel =  new Ext.grid.GridPanel({					  
                        border:false,
						columnLines :true,
                        enableHdMenu : false,
						loadMask : {msg : '数据加载中...'},
                        ds: store,
                        cm: this.cm_partner,						
                        viewConfig: {
                            forceFit:true
                        },
                        tbar:[{
                            text:'增加',
                            tooltip:'增加',
                            iconCls:'add',
                            handler:addPartner
                        }, '-', {
                            text:'修改',
                            tooltip:'修改',
                            iconCls:'option',
                            handler:editPartner
                        },'-',{
                            text:'删除',
                            tooltip:'删除',
                            iconCls:'delete',
                            handler:deletePartner
                        }],
						bbar: new Ext.PagingToolbar({
                            pageSize: 20,
                            store: store,
                            displayInfo: true,
                            plugins: new Ext.ux.ProgressBarPager()
                        })
            });
                      
            
            
              //取得选中的记录
	 		function getSelectedItem(){
	     		var record= partner_gridPanel.getSelectionModel().getSelected();	   
	     		if(record == null){
	        		 Ext.MessageBox.alert('提示','请选择要修改的明细！');
	         		return false;
	     		}else{
	        		return record;
	     		}
	  		}
            
            function addPartner(){
            	if(partner_detailForm.form.el){
            		partner_detailForm.form.el.dom.reset();            	
            	}else{
            		partner_detailForm.form.reset();
            	}
                Ext.getCmp('commitButton').setText('新增');
		        addOrEditPartner_win.isAdd = true;
		        addOrEditPartner_win.setTitle("新增费用项目");
		        addOrEditPartner_win.show();
            }
            
            function editPartner(){
            	if(partner_detailForm.form.el){
            		partner_detailForm.form.el.dom.reset();            	
            	}else{
            		partner_detailForm.form.reset();
            	}
            	var record=getSelectedItem();
            	if(record){
            		Ext.getCmp('commitButton').setText('修改');
		    		addOrEditPartner_win.setTitle("编辑费用项目");
		    		addOrEditPartner_win.isAdd = false;
		    		partner_detailForm.getForm().loadRecord(record);
		    		addOrEditPartner_win.show();
            	}else{
            		 Ext.MessageBox.alert('提示','请选择要修改的明细！');
            	}           	
            }
            
            function deletePartner(){
            }
            
            function submitForm(){
            	if(!partner_detailForm.getForm().isValid()){
	         		 return;
	      		}
	      		if(addOrEditPartner_win.isAdd){
	      		   partner_detailForm.getForm().submit({
	           			url:app.url+'/admin/com/expenseItemAdd.do',
	           			method :"post",
						waitMsg : '正在提交数据,请稍候...',
	          			success:function(response,options){
						 store.reload();
						 Ext.MessageBox.alert("提示","操作成功!");
						 addOrEditPartner_win.hide();
	           			},
	           			failure:function(response,options){
	                		Ext.MessageBox.alert("提示","发生异常，请稍候处理!");    
	           			}
	       			});
	      		}else{
            		partner_detailForm.getForm().submit({
	           			url:app.url+'/admin/com/expenseItemUpdate.do',
	          	 		//params :{queryParam:ordersId},
	           			method :"post",
						waitMsg : '正在提交数据,请稍候...',
	          			success:function(response,options){
						    store.reload();
							Ext.MessageBox.alert("提示","修改成功!");
							
						    addOrEditPartner_win.hide();
	           			},
	           			failure:function(response,options){
	                		Ext.MessageBox.alert("提示","发生异常，请稍候处理!");    
	           			}
	       			});
           		}
         	}
         	
         	return partner_gridPanel;
      },
    createWindow : function(){
        var desktop = this.app.getDesktop();
        var win = desktop.getWindow('expense-win');
        if(!win){
       
            win = desktop.createWindow({
                id: 'partner-win',
                title:'费用项目',
                width:740,
                height:480,
                iconCls: 'icon-grid',
                shim:false,
                animCollapse:false,
                constrainHeader:true,
                layout: 'fit',
                items:this.getPanel()
            });
        }
        win.show();
    }
});